package com.painpointcloud.common.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 */
public class DateUtil {
    private static String datePattern = "yyyy-MM-dd";


    private static String timePattern = "HH:mm";

    /**
     * @param
     */
/*	public static void main(String[] args) {
        //System.out.println(DateUtil.getNowDateTime());//2017-07-06 09:50:37
		//System.out.println(getTimeNow(new Date()));//09:50
		System.out.println(getDateTime("yyyyMMddHHmmss", new Date()));//20170706095116
	}*/
    public static String getNowDateTime() {
        String nowDateTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
        return nowDateTime;
    }

    public static String getNowDate() {
        String nowDateTime = (new SimpleDateFormat(datePattern)).format(new Date());
        return nowDateTime;
    }

    /**
     * This method returns the current date time in the format: yyyy/MM/dd HH:MM
     * a
     *
     * @param theTime the current time
     * @return the current date/time
     */
    public static String getTimeNow(Date theTime) {
        return getDateTime(timePattern, theTime);
    }

    /**
     * This method generates a string representation of a date's date/time in
     * the format you specify on input
     *
     * @param aMask the date pattern the string is in
     * @param aDate a date object
     * @return a formatted string representation of the date
     * @see SimpleDateFormat
     */
    public static final String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null) {
            //log.error("aDate is null!");
        } else {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    /**
     * 时间转换成string
     *
     * @param date
     * @return
     * @author zhangBin
     * @date 2017年7月6日
     */
    public static String dateFormatShort(Date date) {
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
        return dateStr;
    }

    public static String dateFormat(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * string时间转换data
     *
     * @param dateStr
     * @return
     * @throws ParseException
     * @author zhangBin
     * @date 2017年7月6日
     */
    public static Date stringFormatDate(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateStr);
        return date;
    }

    public static int getYearsBetween(String sDate, String bDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return getYearsBetween(sdf.parse(sDate), sdf.parse(bDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int getYearsBetween(Date sDate, Date bDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sDate);
        int sYear = calendar.getWeekYear();
        calendar.setTime(bDate);
        int bYear = calendar.getWeekYear();
        return bYear - sYear;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param sDate 较小的时间
     * @param bDate 较大的时间
     * @return 相差天数
     */
    public static int getDaysBetween(Date sDate, Date bDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            sDate = sdf.parse(sdf.format(sDate));
            bDate = sdf.parse(sdf.format(bDate));
            if (sDate.equals(bDate)) {
                return 1;
            }
            if (sDate.after(bDate)) {
                return 0;
            }
            Calendar cal = Calendar.getInstance();
            cal.setTime(sDate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bDate);
            long time2 = cal.getTimeInMillis();
            Long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
            return betweenDays.intValue() + 1;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int getDaysBetween(String sDate, String bDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return getDaysBetween(sdf.parse(sDate), sdf.parse(bDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static int getDaysBetween(String sDate, String bDate, String patten) {
        SimpleDateFormat sdf = new SimpleDateFormat(patten);
        try {
            return getDaysBetween(sdf.parse(sDate), sdf.parse(bDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * @param date
     * @return Date
     * @throws
     * @Title: getDayFirstTime
     * @Description: 获取一天的开始时间
     * @author zc
     * @date 2015-12-10 下午3:05:44
     */
    public static Date getFirstTimeOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getLastTimeOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 当月第一天
     *
     * @return
     */
    public static Date getFirstDayOfMonth() {
        Calendar firstCal = Calendar.getInstance();
        firstCal.set(Calendar.DAY_OF_MONTH, firstCal.getMinimum(Calendar.DATE));
        return firstCal.getTime();
    }

    /**
     * 当月最后一天
     *
     * @return
     */
    public static Date getLastMonthOfDay() {
        Calendar lastCal = Calendar.getInstance();
        lastCal.set(Calendar.DAY_OF_MONTH, lastCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return lastCal.getTime();
    }


    public static int[] getNeturalAge(Calendar calendarBirth, Calendar calendarNow) {
        int diffYears = 0, diffMonths, diffDays;
        int dayOfBirth = calendarBirth.get(Calendar.DAY_OF_MONTH);
        int dayOfNow = calendarNow.get(Calendar.DAY_OF_MONTH);
        if (dayOfBirth <= dayOfNow) {
            diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
            diffDays = dayOfNow - dayOfBirth;
            if (diffDays != 0) {
                diffDays++;
            }
            /*if (diffMonths == 0)
                diffDays++;*/
        } else {
            if (isEndOfMonth(calendarBirth)) {
                if (isEndOfMonth(calendarNow)) {
                    diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
                    diffDays = 0;
                } else {
                    calendarNow.add(Calendar.MONTH, -1);
                    diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
                    diffDays = dayOfNow + 1;
                }
            } else {
                if (isEndOfMonth(calendarNow)) {
                    diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
                    diffDays = 0;
                } else {
                    calendarNow.add(Calendar.MONTH, -1);// 上个月
                    diffMonths = getMonthsOfAge(calendarBirth, calendarNow);
// 获取上个月最大的一天
                    int maxDayOfLastMonth = calendarNow.getActualMaximum(Calendar.DAY_OF_MONTH);
                    if (maxDayOfLastMonth > dayOfBirth) {
                        diffDays = maxDayOfLastMonth - dayOfBirth + dayOfNow;
                    } else {
                        diffDays = dayOfNow;
                    }
                }
            }
            diffDays++;
        }
// 计算月份时，没有考虑年
        diffYears = diffMonths / 12;
        diffMonths = diffMonths % 12;
        return new int[]{diffYears, diffMonths, diffDays};
    }


    public static int getMonthsOfAge(Calendar calendarBirth,
                                     Calendar calendarNow) {
        return (calendarNow.get(Calendar.YEAR) - calendarBirth
                .get(Calendar.YEAR)) * 12 + calendarNow.get(Calendar.MONTH)
                - calendarBirth.get(Calendar.MONTH);
    }

    public static boolean isEndOfMonth(Calendar calendar) {
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        if (dayOfMonth == calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
            return true;
        return false;
    }

    /**
     * 返回现在本年度，从1月到当月的时间字符串集合
     *
     * @param pattern
     * @return
     */
    public static List<String> nowStrDateList(String pattern) {
        List<String> stringList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        for (int i = 1; i <= month + 1; i++) {
            Calendar c = Calendar.getInstance();
            c.set(year, i, 0);
            stringList.add(sdf.format(c.getTime()));
        }
        return stringList;
    }

    /**
     * 获取过去或者未来 任意天内的日期数组
     *
     * @param intervals intervals天内
     * @return 日期数组
     */
    public static List<String> getPastDateList(int intervals) {
        List<String> pastDaysList = new ArrayList<>();
        for (int i = intervals; i > 0; i--) {
            pastDaysList.add(getPastDate(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(datePattern);
        String result = format.format(today);
        return result;
    }

    /**
     * 在天数加1
     *
     * @param currentDate
     * @return
     */
    public static Date nextDay(Date currentDate) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(currentDate);
        cal.add(GregorianCalendar.DAY_OF_MONTH, 1);// 在天数加1
        return cal.getTime();
    }

    /**
     * 功能：日期String转Date，格式："2012-12-06 "
     * @param dateString
     */
    public static Date dateStringToDate(String dateString){
        Date date =null;
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(dateString);
        }
        catch (ParseException e)
        {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return date;
    }
    /**
     * 判断time是否在from，to之内
     *
     * @param time 指定日期
     * @param from 开始日期
     * @param to   结束日期
     * @return
     */
    public static boolean isBetweenValidTime(Date time, Date from, Date to) {
        if(to==null){
            return true;
        }else{
            to=nextDay(to);//处理日期边界
            Calendar date = Calendar.getInstance();
            date.setTime(time);

            Calendar after = Calendar.getInstance();
            after.setTime(from);

            Calendar before = Calendar.getInstance();
            before.setTime(to);
            if (date.after(after) && date.before(before)) {
                return true;
            } else {
                return false;
            }
        }
    }

    /*public static void main(String[] args) throws ParseException {

        //System.out.println(dateStringToDate("2017-07-06"));
        Date start=dateStringToDate("2017-8-17");
        Date end=dateStringToDate("2017-8-18");
        Date now=new Date();
        System.out.println(isBetweenValidTime(now,start,end));
       *//* int day = getDaysBetween("2017-07-06", "2017-12-31");
        System.out.println(day);
        System.out.println(day / 30);
        System.out.println(day % 30);
        Date date = DateUtils.addDays(new Date(), 2);

        Date addDate = DateUtils.addDays(date, 150);
        System.out.println(addDate);

        System.out.println(DateUtils.addDays(addDate, 29));*//*

       *//* int day = getDaysBetween("2017-07-31", "2017-12-27");
        System.out.println(day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c1 = Calendar.getInstance();
        c1.setTime(sdf.parse("2017-03-01"));
        Calendar c2 = Calendar.getInstance();

        c2.setTime(sdf.parse("2018-03-01"));
        int[] ne = getNeturalAge(c1, c2);


        System.out.println(JSON.toJSON(ne));

        System.out.println(getYearsBetween("2015-01-01", "2017-01-01"));*//*

  *//*      Date curDate = getFirstTimeOfDay(new Date());
        Date firstDay = getFirstTimeOfDay(getFirstDayOfMonth());
        Date secDay = DateUtils.addDays(firstDay, 1);
        Date lastDay = getFirstTimeOfDay(getLastMonthOfDay());


        System.out.println(curDate);
        System.out.println(firstDay);
        System.out.println(secDay);
        System.out.println(lastDay);*//*
    }
*/

}
